import Vue from 'vue'
import Vuex from 'vuex'
import {fetch} from '../config/fetch.js'
import {ACCOUNT_USERINFO, HOT_GOODS_WORD, HOT_BRAND_WORD, SHELVES_GOODS_COUNT,CHANGE_IS_BILL} from './mutations-type.js'
import {hotWords, getGoodsCount} from '../config/interface.js'

Vue.use(Vuex)

 const store = new Vuex.Store({
	state: {
		userId: localStorage.getItem("userId"), //用户信息
		account: localStorage.getItem("account"),
		token: localStorage.getItem("token"),
		logoPath: localStorage.getItem("logoPath"),
		hotGoodsWord: {}, //商品热搜词
	    hotBrandWord: {}, //品牌热搜词
	    goodsCount: '', //货架数量
	    goodsCountShow: true,//是否显示数字
	    appId: localStorage.getItem("appId"),
	    isBill: 0,  // 显示含税商品还是不含税商品，0-不含税，1-含税
	    colorMain: '#ffa726'
	},
	getters: {},
	actions: {
		setUserInfo({commit}, res){
			localStorage.setItem("userId", res.userId);
			localStorage.setItem("account", res.account);
			localStorage.setItem("token", res.token);
			localStorage.setItem("logoPath", res.logoPath);
			localStorage.setItem("appId", res.appId);
			commit(ACCOUNT_USERINFO, res);
		},
		setHotGoodsWord(context, res){
			var url = hotWords;
			var data = {
				count: '',
    			type: 1,
    			userId: context.state.userId
			};
			fetch(url, data)
				.then(res => {
                    context.commit(HOT_GOODS_WORD, res.data)
                });
		},
		setHotBrandWord(context, res){
			var url = hotWords;
			var data = {
				count: '',
    			type: 2,
    			userId: context.state.userId
			};
			fetch(url, data)
				.then(res => {
                    context.commit(HOT_BRAND_WORD, res.data)
                });
		},
		setGoodsCount(context, res){
			var url = getGoodsCount;
			var data = {
				appId: context.state.appId,
				userId: context.state.userId
			};
			fetch(url, data)
				.then(res => {
                    context.commit(SHELVES_GOODS_COUNT, res.data)
                });
		},
		changeIsBill({commit}, bill) {
			commit(CHANGE_IS_BILL, bill)
		}		
	},
	mutations: {
		[ACCOUNT_USERINFO](state, res){
			state.userId = res.userId;
			state.account = res.account;
			state.token = res.token;
			state.logoPath = res.logoPath;
			state.appId = res.appId;
		},
		[HOT_GOODS_WORD](state, res){
			state.hotGoodsWord = res
		},
		[HOT_BRAND_WORD](state, res){
			state.hotBrandWord = res
		},
		[SHELVES_GOODS_COUNT](state, res){
			if(res == 0){
				state.goodsCountShow = true;
			}else{
				state.goodsCountShow = false;
			}
			state.goodsCount = res;
		},
		[CHANGE_IS_BILL] (state, bill) {
			state.isBill = bill;
		}
	}
})

 export default store